package com.gym.baselib.utils

import android.annotation.SuppressLint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.gym.baselib.BaseApplication
import com.xgst.lib.R
import com.xgst.lib.utils.TimeUtils
import io.reactivex.rxjava3.disposables.Disposable

/**
 *创建人：东
 *时间：2021/1/28
 */
@SuppressLint("MissingInflatedId")
object ProgressUtils{

    private var toast:Toast? = null
    private var disposable: Disposable ? = null
    private val view:View
    private val tvProgress:TextView

    init {
        view = LayoutInflater.from(BaseApplication.context).inflate(R.layout.progress_layout,null)
        tvProgress = view.findViewById(R.id.tvProgress)
    }

    fun showProgress(isLoop:Boolean = false){
        show()
        disposable?.dispose()
        disposable = TimeUtils.countDownOneTime(3*1000,{
            if(isLoop){
                showProgress()
            }else{
                show("请稍后，加载中...")
            }
        })
    }

   private fun show(content:String = "加载中..."){
        disposable?.dispose()
        toast?.cancel()
        toast = Toast(BaseApplication.context)
        toast!!.let {
            it.setGravity(Gravity.CENTER,0,0)
            it.duration = Toast.LENGTH_LONG
            it.view = view
            tvProgress.text = content
            it.show()
        }
    }

    fun dismiss(){
        disposable?.dispose()
        toast?.cancel()
    }
}

